package com.example.service_order.utils;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.*;

/**
 * @Author:tjf
 * @Description:
 */
public class OrderNoUtils {
    /**
     * 获取订单号
     *
     * @return
     */
    public static String getOrderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }

    /**
     * 商户号
     */
    public static String merchantId = "1558950191";
    /**
     * 商户API私钥路径
     */
    public static String privateKeyPath = "service/service_order/apiclient_key.pem";
    /**
     * 商户证书序列号
     */
    public static String merchantSerialNumber = "34345964330B66427E0D3D28826C4993C77E631F";
    /**
     * 商户APIV3密钥
     */
    public static String apiV3Key = "UDuLFDcmy5Eb6o0nTNZdu6ek4DDh4K8B";

        public static void main(String[] args) {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 构建service
        NativePayService service = new NativePayService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
//        PrepayRequest request = new PrepayRequest();
//        Amount amount = new Amount();
//        amount.setTotal(1);
//        request.setAmount(amount);
//        request.setAppid("wx74862e0dfcf69954");
//        request.setMchid("1558950191");
//        request.setDescription("测试商品标题");
//        request.setNotifyUrl("http://461i1081c3.wicp.vip:24970");
//        request.setOutTradeNo("out_178874552");
//        // 调用下单方法，得到应答
//        PrepayResponse response = service.prepay(request);
//        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
//        System.out.println("222" + response.getCodeUrl());

            QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
            request.setMchid(merchantId);
            request.setOutTradeNo("20240310222143739");

            try {
                Transaction result = service.queryOrderByOutTradeNo(request);
                System.out.println(result);
            } catch (ServiceException e) {
                // API返回失败, 例如ORDER_NOT_EXISTS
                System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
                System.out.printf("reponse body=[%s]\n", e.getResponseBody());
            }
    }

}
